#include "event/znx_proc.h"
#include "conf/znx_core_conf.h"
#include "event/znx_worker_proc.h"
#include "comm/znx_cpuinfo.h"

#if (ZNX_EXP_MODULE)

#include "exp/exp.h"

void test_exp_module()
{
    ZNX_DEF_LOG_DEBUG(NULL, "test exp module");

    znx_exp_module_conf_t   *exp_conf;
    exp_conf = znx_core_get_module_conf(exp_core_module);
    if (exp_conf == NULL) {
        ZNX_DEF_LOG_FATAL(NULL, "not found exp conf");
    }

    if (exp_conf->e1 != 123) {
        ZNX_DEF_LOG_FATAL(NULL, "expect 123, but got %d", exp_conf->e1);
    }

    if (exp_conf->e2.len != 8 ||
        znx_strncmp(exp_conf->e2.data, "exp_test", 8) != 0)
    {
        ZNX_DEF_LOG_FATAL(NULL, "expect exp_test, but got %v", &exp_conf->e2);
    }

    if (!exp_conf->e3) {
        ZNX_DEF_LOG_FATAL(NULL, "expect true, but got false");
    }
}


#endif


int main()
{
    znx_proc_init();

    if (!znx_core_conf_parse("/tmp/core_exp.yaml")) {
        if (g_core_conf) {
            ZNX_DEF_LOG_FATAL(NULL, "parse /tmp/core_exp.yaml failed, err: %v",
                &g_core_conf->err);
        }
        ZNX_DEF_LOG_FATAL(NULL, "parse /tmp/core_exp.yaml failed, err: unknow");
    }

    if (g_core_conf->g_mem_freelists_level > 0) {
        znx_mem_init(g_core_conf->g_mem_freelists_level);
    }

    if (g_core_conf->g_aligned_mem_freelists_level > 0) {
        znx_mem_init(g_core_conf->g_aligned_mem_freelists_level);
    }

    znx_modules_main_init_exec();

    if (g_core_conf->worker_threads != znx_cpu_num()) {
        ZNX_DEF_LOG_FATAL(NULL, "expect %d, but got %d",
            znx_cpu_num(), g_core_conf->worker_threads);
    }

    if (g_core_conf->tcp_accepter_threads != 2) {
        ZNX_DEF_LOG_FATAL(NULL, "expect 2, but got %d",
            g_core_conf->tcp_accepter_threads);
    }

    if (g_core_conf->g_mem_freelists_level != 8) {
        ZNX_DEF_LOG_FATAL(NULL, "expect 8, but got %d",
            g_core_conf->g_mem_freelists_level);
    }

    if (g_core_conf->g_aligned_mem_freelists_level != 4) {
        ZNX_DEF_LOG_FATAL(NULL, "expect 4, but got %d",
            g_core_conf->g_aligned_mem_freelists_level);
    }

    if (g_core_conf->worker_mem_freelist_level != 16) {
        ZNX_DEF_LOG_FATAL(NULL, "expect 100, but got %d",
            g_core_conf->worker_mem_freelist_level);
    }

    if (g_core_conf->worker_aligned_mem_freelists_level != 8) {
        ZNX_DEF_LOG_FATAL(NULL, "expect 200, but got %d",
            g_core_conf->worker_aligned_mem_freelists_level);
    }


#if (ZNX_EXP_MODULE)
    test_exp_module();
#endif

    znx_worker_processor_start(2, 0, 0);

    usleep(10000);

    znx_proc_shut_down();
    return 0;
}